package filetransTest;

import org.junit.Test;

import java.io.*;
import java.net.*;
import java.util.Arrays;

/**
 * @author 李泽伟 实现文件的TCP传输
 */
public class MainTest {
    /**
     * 客户端发送数据
     */
    @Test
    public void clientFileOutput() {
        //创建一个字节流文件写出
        File file = new File("test.mp4");
        System.out.println(file.length());
        FileInputStream os = null;
        BufferedInputStream fileInputStream = null;
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            os = new FileInputStream(file);
            fileInputStream = new BufferedInputStream(os);
            //创建客户端socket
            socket = new Socket(InetAddress.getByName("localhost"), 8899);
            outputStream = socket.getOutputStream();
            //tcp发送文件信息
//            outputStream.write((int) file.length());
            //文件写入内存、tcp发送文件数据
//            Thread.sleep(1000);
            byte[] buf = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            //read()方法阻塞了，需要关闭
            socket.shutdownOutput();
            InputStream inputStream = socket.getInputStream();
            byte[] buf2 = new byte[1024];
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            while ((len = inputStream.read(buf2))!=-1){
                bs.write(buf2, 0, len);
            }
            System.out.println(bs.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void serverFileInput() {
        //1.文件写出流
        File file = new File("server.mp4");
        FileOutputStream fileOutputStream = null;
        ServerSocket ss = null;
        Socket socket = null;
        InputStream inputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            //2.创建服务器tcp
            ss = new ServerSocket(8899);
            socket = ss.accept();
            inputStream = socket.getInputStream();
            //接收文件大小信息
            int len;
            //  int len = inputStream.read();
            //  byte[] bytes = inputStream.readNBytes(32);
            //  System.out.println("文件大小：" + Arrays.toString(bytes));
            // 循环接收文件
            byte[] buf = new byte[1024];
            while ((len = inputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, len);
            }
            //发送已收到的提示
            System.out.println("文件接收完成");
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("文件已收到！".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (ss != null) {
                    ss.close();
                }
                if (socket != null) {
                    socket.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
